package com.fire.web.modules.all.controller;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.web.modules.all.utils.WrapperUtil;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.service.ITMitErrorCodeService;
import com.fire.web.utils.PageRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class CrudController<Service extends IService, Entity extends Model> {
	private static final Logger LOGGER = LoggerFactory.getLogger(CrudController.class);
	@Autowired
	protected Service service;
	@Autowired
	ITMitErrorCodeService errorCodeService;


	/**
	 * 查询列表
	 *
	 * @return
	 */
	@GetMapping("/page")
	public AjaxResult page(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult ajaxResult;
		try {
			//遍历参数获得查询条件
			EntityWrapper ew = WrapperUtil.getEw(params);
			//查询
			Page pageBean = service.selectPage(PageRequest.getPage(), ew);
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", pageBean, "获取成功");
		} catch (Exception e) {
			LOGGER.error("CrudController.page().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	/**
	 * 删除
	 *
	 * @return
	 */
	@DeleteMapping("/delete/{id}")
	@ResponseBody
	public AjaxResult delete(@PathVariable("id") String id) {
		AjaxResult ajaxResult;
		try {
			try {
				service.deleteById(Integer.parseInt(id));
				ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "删除成功", "删除成功");
			} catch (Exception e) {
				service.deleteById(id);
				ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "删除成功", "删除成功");
			}

		} catch (Exception e) {
			LOGGER.error("Controller.delete().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	/**
	 * 增加业主单位
	 *
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult add(Entity entity) {
		AjaxResult ajaxResult;
		try {
			service.insert(entity);
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "新增成功", "新增成功");
		} catch (Exception e) {
			LOGGER.error("CrudController.add().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	/**
	 * 更新业主单位
	 *
	 * @return
	 */
	@PostMapping("/update")
	@ResponseBody
	public AjaxResult update(Entity entity) {
		AjaxResult ajaxResult;
		try {
			service.updateById(entity);
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "更新成功", "更新成功");
		} catch (Exception e) {
			LOGGER.error("CrudController.update().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	protected AjaxResult replaceTips(AjaxResult ajaxResult) {
		String tips = errorCodeService.getTips(ajaxResult.getStatus());
		if (StringUtils.isNotBlank(tips)) {
			ajaxResult.setTips("[" + ajaxResult.getStatus() + "]" + tips);
		} else {
			ajaxResult.setTips("[" + ajaxResult.getStatus() + "]" + ajaxResult.getTips());
		}
		return ajaxResult;
	}

}
